﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.iss.inventory.Controllers;
using System.Drawing;

namespace Staionery_Inventory.Presentation.Admin
{
    public partial class DepartmentMaintenanceUI__Reload_ : System.Web.UI.Page
    {
        public DepartmentMaintenanceController deptCon = new DepartmentMaintenanceController();
        public List<String> allDeptID;
        private int pointer = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            //security and assign name
            if (Session["empID"] == null)
            {
                Response.Redirect("~/Presentation/login.aspx");
            }

            Label lbl = this.Master.FindControl("lblLogInName") as Label;
            if (lbl != null)
            {
                lbl.Text = Session["UserName"].ToString();
            }
            //

            allDeptID = deptCon.getAllDeptID();
            if (Session["currentPointer"] == null)
            {
                lblMessage.Text = "Click ◀ and ▶ button for sequence browsing";
                Session["currentPointer"] = 0;
                String[] department = deptCon.getDeptByID(allDeptID[pointer].ToString());
                setDataToTextboxes(department);
            }
            pointer = Convert.ToInt32(Session["currentPointer"]);
        }

        protected void btnPrev_Click(object sender, EventArgs e)
        {
            if (pointer == 0) { pointer = allDeptID.Count(); }
            pointer -= 1;
            String[] department = deptCon.getDeptByID(allDeptID[pointer].ToString());
            Session["currentPointer"] = Convert.ToString(pointer);
            setDataToTextboxes(department);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            if (pointer == allDeptID.Count() - 1) { pointer = -1; }
            pointer += 1;
            String[] department = deptCon.getDeptByID(allDeptID[pointer].ToString());
            Session["currentPointer"] = Convert.ToString(pointer);
            setDataToTextboxes(department);
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            if (btnCreate.Text == "New")
            {
                txtDeptName.ReadOnly = false; txtContactName.ReadOnly = false; txtPhone.ReadOnly = false; txtFax.ReadOnly = false; txtHeadID.ReadOnly = false; txtCollectionPoint.ReadOnly = false; txtRepresentativeID.ReadOnly = false;
                clearFields();
                btnCreate.Text = "Save";
            }
            else if (btnCreate.Text == "Save")
            {
                String result = deptCon.createDept(txtDeptName.Text, txtContactName.Text, txtPhone.Text, txtFax.Text, txtHeadID.Text, txtCollectionPoint.Text, txtRepresentativeID.Text);
                if (result == "Create Department Successfully")
                {
                    allDeptID = deptCon.getAllDeptID();
                    lblMessage.ForeColor = Color.Black;
                    lblMessage.Text = result;
                    txtDeptID.Text = allDeptID.Last().ToString();
                    txtDeptName.ReadOnly = true; txtContactName.ReadOnly = true; txtPhone.ReadOnly = true; txtFax.ReadOnly = true; txtHeadID.ReadOnly = true; txtCollectionPoint.ReadOnly = true; txtRepresentativeID.ReadOnly = true;
                    btnCreate.Text = "New";
                    Session["currentPointer"] = allDeptID.Count() - 1;
                }
                else
                {
                    lblMessage.ForeColor = Color.Red;
                    lblMessage.Text = result;
                }
            }

        }

        public void setDataToTextboxes(String[] department)
        {
            txtDeptID.Text = department[0]; txtDeptName.Text = department[1]; txtContactName.Text = department[2]; txtPhone.Text = department[3]; txtFax.Text = department[4]; txtHeadID.Text = department[5]; txtCollectionPoint.Text = department[6]; txtRepresentativeID.Text = department[7];
        }

        public void clearFields()
        {
            txtDeptID.Text = ""; txtDeptName.Text = ""; txtContactName.Text = ""; txtPhone.Text = ""; txtFax.Text = ""; txtHeadID.Text = ""; txtCollectionPoint.Text = ""; txtRepresentativeID.Text = "";
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            String result = deptCon.deleteDept(txtDeptID.Text);
            allDeptID = deptCon.getAllDeptID();
            if (pointer != 0) { pointer -= 1; }
            Session["currentPointer"] = pointer;
            String[] department = deptCon.getDeptByID(allDeptID[pointer].ToString());
            setDataToTextboxes(department);

            lblMessage.ForeColor = Color.Black;
            lblMessage.Text = result;
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (btnUpdate.Text == "Update")
            {
                txtDeptName.ReadOnly = false; txtContactName.ReadOnly = false; txtPhone.ReadOnly = false; txtFax.ReadOnly = false; txtHeadID.ReadOnly = false; txtCollectionPoint.ReadOnly = false; txtRepresentativeID.ReadOnly = false;
                String tempSave = txtDeptID.Text;
                txtDeptID.Text = tempSave;
                btnUpdate.Text = "Save";
            }
            else if (btnUpdate.Text == "Save")
            {
                String result = deptCon.updateDept(txtDeptID.Text, txtDeptName.Text, txtContactName.Text, txtPhone.Text, txtFax.Text, txtHeadID.Text, txtCollectionPoint.Text, txtRepresentativeID.Text);
                if (result == "Update Department Successfully")
                {
                    lblMessage.ForeColor = Color.Black;
                    lblMessage.Text = result;
                    txtDeptName.ReadOnly = true; txtContactName.ReadOnly = true; txtPhone.ReadOnly = true; txtFax.ReadOnly = true; txtHeadID.ReadOnly = true; txtCollectionPoint.ReadOnly = true; txtRepresentativeID.ReadOnly = true;
                    btnUpdate.Text = "Update";
                    allDeptID = deptCon.getAllDeptID();
                }
                else
                {
                    lblMessage.ForeColor = Color.Red;
                    lblMessage.Text = result;
                }
            }
        }
    }
}